package app.service.sys;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import app.model.sys.SysMenu;
import base.PageInfo;
import base.category.sys.YesOrNo;
import base.dao.HibernateDao;

@Repository
public class SysMenuService extends HibernateDao {

	public List<Map<String,Object>> queryMenuList(boolean withDisableData) {
		String sql = "";
		if(withDisableData){
			sql = "select * from sys_menu a order by a.menu_code";
		}else{
			sql = "select * from sys_menu a where a.is_use = " + YesOrNo.YES + " order by a.menu_code";
		}
		List<Map<String,Object>> queryListBySql = queryListBySql(sql);
		return queryListBySql;
	}

	public PageInfo queryPageList(PageInfo pageInfo) {
		String sql = "select * from sys_menu a where a.is_use = " + YesOrNo.YES + " order by a.menu_code";
		PageInfo page = queryPageListBySql(pageInfo, sql);
		return page;
	}

	public SysMenu queryEntityById(String id) {
		String sql = "select * from Sys_Menu a where a.id = '"+id+"'";
		Object entity = queryEntityBySql(sql , SysMenu.class);
		return (SysMenu)entity;
	}

	public void delEntity(String ids) {
		if(!StringUtils.isEmpty(ids)){
			String[] idsArray = ids.split(",");
			for (int i = 0; i < idsArray.length; i++) {
				SysMenu gb = new SysMenu();
				gb.setId(idsArray[i]);
				del(gb);
			}
		}
	}

	public void saveEntity(SysMenu sysMenu) {
		save(sysMenu);
	}

	public void updateEntity(SysMenu sysMenu) {
		mergeAndUpdate(sysMenu);
	}

	public void enable(String id) {
		SysMenu entity = queryEntityById(id);
		entity.setIsUse(YesOrNo.YES_INT);
		mergeAndUpdate(entity);
	}

	public void disable(String id) {
		SysMenu entity = queryEntityById(id);
		entity.setIsUse(YesOrNo.NO_INT);
		mergeAndUpdate(entity);
	}
}
